<template>
<div class="box">
  <div class="form">
     <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-form-item label="account" prop="pass">
      <el-input v-model="ruleForm.pass" type="text" autocomplete="off" />
    </el-form-item>
    <el-form-item label="password" prop="checkPass">
      <el-input
        v-model="ruleForm.checkPass"
        type="password"
        autocomplete="off"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)"
        >Submit</el-button
      >
      <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
    </el-form-item>
  </el-form>
  </div>
</div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import {useRouter} from "vue-router"
const router = useRouter()

const ruleFormRef = ref()
const formLabelWidth =120

const form = reactive({
  name:"",
  img:"",
  type:"",
  num:"",
  mp4:"",
  code:"",
  pwd:"",
  account:""
})
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please input the account'))
  } else {
    if (ruleForm.checkPass !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass', () => null)
    }
    if(value=="123")
    callback()
    else callback(new Error("account is wrong"))
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('Please input the password again'))
  } else if (value !== ruleForm.pass) {
    callback(new Error("Two inputs don't match!"))
  } else {
    callback()
  }
}

const ruleForm = reactive({
  pass: '',
  checkPass: '',

})

const rules = reactive({
  pass: [{ validator: validatePass, trigger: 'blur' }],
  checkPass: [{ validator: validatePass2, trigger: 'blur' }],

})

const submitForm = formEl=> {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      window.sessionStorage.setItem(
        "isLogin",true
      )
      router.push("/")

    } else {
      console.log('error submit!')
      return false
    }
  })
}

const resetForm = formEl => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>

.box{
  width: 100%;
  height: 100%;
}
.form{
  width: 80%;
  margin:20px auto;
}
</style>